package com.bum.api.bo.helper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class RssHelper {
	static Pattern	rssPattern	= Pattern.compile("rss://((?:[a-z0-9]+[a-zA-Z0-9\\-_/\\.]+))(\\b|\\s)", Pattern.CASE_INSENSITIVE);

	public static String getUrl(String meContent) {
		if (StringUtils.isBlank(meContent)) {
			return null;
		}

		Matcher matcher = rssPattern.matcher(meContent);
		if (matcher.find()) {
			return matcher.group(1);
		}

		return null;
	}

	private static final String	MEDIA_REGEX		= "\\B#" + (char) 27 + "((?:[0-9][PSVAO][a-z0-9][a-zA-Z0-9]{3}[u0-9]?[a-zA-Z0-9]{7,}))#";
	static Pattern				MEDIA_PATTERN	= Pattern.compile(MEDIA_REGEX);

	public static String getCover(String content) {
		if (StringUtils.isBlank(content)) {
			return null;
		}

		Matcher matcher = MEDIA_PATTERN.matcher(content);
		while (matcher.find()) {
			String targetedUser = matcher.group(0).substring(1).replace("#", "").trim();
			return targetedUser;
		}
		return null;
	}

	private static final int	INTRO_MAX_LENGTH	= 300;

	public static String getIntro(String content) {
		if (StringUtils.isBlank(content)) {
			return null;
		}

		String intro = content;
		intro = intro.replaceAll(MEDIA_REGEX, "").replaceAll("\n", " ") + " ";
		if (intro.length() > INTRO_MAX_LENGTH) {
			int endIdx = intro.indexOf(" ", INTRO_MAX_LENGTH - 5);
			intro = intro.substring(0, endIdx);
		}

		return intro.trim() + "...";
	}
}
